﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ClassLibrary.App_Code;

/// <summary>
/// Summary description for CartsDAL
/// </summary>
public class CartsDAL
{
    public static ShopMobileDataContext data = new ShopMobileDataContext();
	public CartsDAL()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static bool insertToCart(CartsBLL cart)
    {
        Cart c = new Cart();
        c.add_on = DateTime.Now;
        c.status_id = 0;
        c.name = cart.name;
        c.email = cart.email;
        c.address = cart.address;
        c.phone_number = cart.phone_number;
        c.user_id = cart.user_id;
        c.cash_sum = cart.cash_sum;
        c.payment = cart.payment;
        data.Carts.InsertOnSubmit(c);
        data.SubmitChanges();
        return true;
    }

    public static int getMaxCartId()
    {
        var id = from cart in data.Carts
                 select cart.id;
        return id.Max();
    }

    public static void insertCartDetail(Cart_itemBLL cart, int cartId)
    {
        Cart_item cart_item = new Cart_item();
        cart_item.add_on = DateTime.Now;
        cart_item.cart_id = cartId;
        cart_item.item_id = cart.item_id;
        cart_item.quantity = cart.quantity;
        cart_item.cash_total = cart.cash_total;
        data.Cart_items.InsertOnSubmit(cart_item);
        data.SubmitChanges();
    }

    public static List<CartsBLL> getCartStt(string status)
    {
        switch (status)
        {
            case "Chưa giao":
                var d1 = from cart in data.Carts
                         where cart.Status.name_stt == status
                         orderby cart.status_id ascending
                         select new CartsBLL(cart.id, cart.add_on.Value, cart.Status.name_stt, cart.name, cart.address, cart.phone_number, cart.cash_sum.Value);
                return d1.ToList<CartsBLL>();

            case "Đã giao":
                var d2 = from cart in data.Carts
                         where cart.Status.name_stt == status
                         orderby cart.status_id ascending
                         select new CartsBLL(cart.id, cart.add_on.Value, cart.status_id.Value, cart.name, cart.email, cart.address, cart.note, cart.phone_number, cart.user_id.Value, cart.cash_sum.Value, cart.payment.Value, cart.Status.name_stt);
                return d2.ToList<CartsBLL>();
            case "Hủy đơn hàng":
                var d3 = from cart in data.Carts
                         where cart.Status.name_stt == status
                         orderby cart.status_id ascending
                         select new CartsBLL(cart.id, cart.add_on.Value, cart.status_id.Value, cart.name, cart.email, cart.address, cart.note, cart.phone_number, cart.user_id.Value, cart.cash_sum.Value, cart.payment.Value, cart.Status.name_stt);
                return d3.ToList<CartsBLL>();
            case "Tất cả":
                var ds4 = from cart in data.Carts
                         orderby cart.status_id ascending
                         select new CartsBLL(cart.id, cart.add_on.Value, cart.status_id.Value, cart.name, cart.email, cart.address, cart.note, cart.phone_number, cart.user_id.Value, cart.cash_sum.Value, cart.payment.Value, cart.Status.name_stt);
                return ds4.ToList<CartsBLL>();
            default:
                var ds = from cart in data.Carts
                         orderby cart.status_id ascending
                         select new CartsBLL(cart.id, cart.add_on.Value, cart.status_id.Value, cart.name, cart.email, cart.address, cart.note, cart.phone_number, cart.user_id.Value, cart.cash_sum.Value, cart.payment.Value, cart.Status.name_stt);
                return ds.ToList<CartsBLL>();

        }
    }
    public static CartsBLL getCartDetail(int cart_id)
    {
        Cart cart = data.Carts.Single(c => c.id == cart_id);
        return  new CartsBLL(cart.id, cart.add_on.Value, cart.Status.name_stt, cart.name, cart.address, cart.phone_number, cart.cash_sum.Value);
    }

    public static void setStatusCart(int cart_id, int id_Stt)
    {
        Cart cart = data.Carts.Single(c => c.id == cart_id);
        cart.status_id = id_Stt;
        data.SubmitChanges();
    }
    public static int totalSum(DateTime f, DateTime t)
    {
        var car = from c in data.Carts
                  where c.add_on.Value.Date >= f.Date && c.add_on.Value.Date <=t.Date
                  select c.cash_sum.Value;
        return car.Sum();
    }
    public static int MaxCart(DateTime f, DateTime t)
    {
        var car = from c in data.Carts
                  where c.add_on.Value.Date >= f.Date && c.add_on.Value.Date <= t.Date
                  select c.cash_sum.Value;
        return car.Max();
    }
}